package org.example;
//import org.springframework.context.ApplicationContext;
//import org.springframework.context.support.ClassPathXmlApplicationContext;
//import java.util.List;
//public class Main {
//    public static void main(String[] args) {
//        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//        DogDao dogDao = (DogDao) context.getBean("dogDao");
//
//        Dog dog1 = new Dog("qwa1", 5);
//        dogDao.addDog(dog1);
//        Dog dog3 = new Dog("nmd1", 7);
//        dogDao.addDog(dog3);
//        Dog dog2 = dogDao.getDog("qwa1");
//        System.out.println(dog2);
//
//        dog2.setAge(15);
//        dogDao.updateDog(dog2);
//        System.out.println(dog2);
//
//
//        List<Dog> dogs = dogDao.getAllDogs();
//        for (Dog dog : dogs) {
//            System.out.println(dog);
//        }
//        dogDao.deleteDog("nmd1");
//    }
//}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class Main extends JFrame {
    private RoomManager roomManager = new RoomManager();
    private JTextArea roomListArea;
    private JTextField roomIdField;
    private JTextField guestNameField;
    private JTextField checkInDateField;
    private JTextField checkOutDateField;
    private JLabel statusLabel;
    private void checkOutRoom() {
        String roomIdStr = JOptionPane.showInputDialog(this, "请输入要退房的房间ID:");

        try {
            int roomId = Integer.parseInt(roomIdStr);
            boolean checkoutSuccess = roomManager.checkOutRoom(roomId);
            JOptionPane.showMessageDialog(this, checkoutSuccess ? "退房成功!" : "退房失败.");
            updateRoomList(); // 更新房间列表以反映更改
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "请输入有效的房间ID.");
        }
    }
    public Main() {
        setTitle("房间管理系统");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        JButton checkOutButton = new JButton("退房");
        checkOutButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                checkOutRoom();
            }
        });


        roomListArea = new JTextArea();
        roomListArea.setEditable(false);
        updateRoomList();

        JPanel inputPanel = new JPanel(new GridLayout(5, 2));
        inputPanel.add(new JLabel("房间ID:"));
        roomIdField = new JTextField(10);
        inputPanel.add(roomIdField);

        inputPanel.add(new JLabel("客人姓名:"));
        guestNameField = new JTextField(20);
        inputPanel.add(guestNameField);

        inputPanel.add(new JLabel("入住日期 (yyyy-MM-dd):"));
        checkInDateField = new JTextField(10);
        inputPanel.add(checkInDateField);

        inputPanel.add(new JLabel("退房日期 (yyyy-MM-dd):"));
        checkOutDateField = new JTextField(10);
        inputPanel.add(checkOutDateField);

        JButton bookButton = new JButton("预订房间");
        bookButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                bookRoom();
            }
        });

        statusLabel = new JLabel();
        statusLabel.setForeground(Color.RED);

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(new JScrollPane(roomListArea), BorderLayout.CENTER);
        getContentPane().add(inputPanel, BorderLayout.NORTH);
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(bookButton);
        buttonPanel.add(statusLabel);
        buttonPanel.add(checkOutButton);
        getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    }

    private void updateRoomList() {
        List<Room> rooms = roomManager.getAllRooms();
        StringBuilder sb = new StringBuilder("所有房间列表:\n");
        for (Room room : rooms) {
            sb.append("房间ID: ").append(room.getRoomId()).append(", ")
                    .append("房间号: ").append(room.getRoomNumber()).append(", ")
                    .append("房间类型: ").append(room.getRoomType()).append(", ")
                    .append("每晚价格: ").append(room.getPricePerNight()).append(", ")
                    .append("是否可用: ").append(room.isAvailable() ? "是" : "否").append("\n");
        }
        roomListArea.setText(sb.toString());
    }

    private void bookRoom() {
        String roomIdStr = roomIdField.getText();
        String guestName = guestNameField.getText();
        String checkInDateStr = checkInDateField.getText();
        String checkOutDateStr = checkOutDateField.getText();

        try {
            int roomId = Integer.parseInt(roomIdStr);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date checkInDate = sdf.parse(checkInDateStr);
            Date checkOutDate = sdf.parse(checkOutDateStr);

            boolean bookingSuccess = roomManager.bookRoom(roomId, guestName, checkInDate, checkOutDate);
            statusLabel.setText(bookingSuccess ? "房间预订成功!" : "房间预订失败.");
        } catch (NumberFormatException e) {
            statusLabel.setText("请输入有效的房间ID.");
        } catch (ParseException e) {
            statusLabel.setText("日期格式错误，请使用 yyyy-MM-dd 格式.");
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Main().setVisible(true);
            }
        });
    }
}